package com.android.fluttercontainer;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.Map;

/**
 * 创建者 ：  huyangyang962
 * 创建日期： 2021/5/24 4:27 PM
 * 描述：
 */
public class SchmelUtils {


    private static volatile SchmelUtils sInstace = null;

    private SchmelUtils() {

    }

    public static SchmelUtils getInstance() {
        if (sInstace == null) {
            synchronized (SchmelUtils.class) {
                if (sInstace == null) {
                    sInstace = new SchmelUtils();
                }
            }
        }
        return sInstace;
    }


    public static void goToActivity(Context context, String pageName, Map<String, Object> params) {
        if ("groupDetail".equals(pageName)) {
            if (params.get("groupId") != null) {
                String groupId = (String) params.get("groupId");
                String index = (String) params.get("index");
                goToGroupDetail(context, groupId, index);
            }
        } else if ("regist".equals(pageName)) {
            goToRegist(context);
        } else if ("h5Page".equals(pageName)) {
            if (params.get("h5Url") != null) {
                String h5Url = (String) params.get("h5Url");
                goToH5Page(context, h5Url);
            }
        } else if ("apmApp".equals(pageName)) {
            jumpToApmApp();
        }
    }

    private static void jumpToApmApp() {
        if (AppUtils.isAppInstalled("com.gapsk.aet")) {
            Intent intent = FlutterApplication.sApplication.getPackageManager().getLaunchIntentForPackage("com.gapsk.aet");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            FlutterApplication.sApplication.startActivity(intent);
        } else {
            transferToGooglePlay("com.gapsk.aet");
        }
    }

    public static void goToGroupDetail(Context context, String groupId, String index) {
        SPUtils.getInstance("GAPSK_AI_QM").put("GROUP_ID", groupId + "," + index);

        Intent intent = new Intent();
        intent.setClassName(context, "com.zd.kids.ui.activity.BookSeriesListActivity");
        intent.putExtra("GROUP_ID_KEY", groupId);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void goToRegist(Context context) {
        Intent intent = new Intent();
        intent.setClassName(context, "com.zd.kids.ui.activity.InputBookCodeActivity");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void goToH5Page(Context context, String url) {
        Intent intent = new Intent();
        intent.setClassName(context, "com.zd.kids.ui.activity.CommonH5Activity");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("URL_PATH_KEY", url);
        context.startActivity(intent);
    }

    private static void transferToGooglePlay(String packName) {
        try {
            Uri uri = Uri.parse("market://details?id=" + packName);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            if (!TextUtils.isEmpty("com.android.vending")) {
                intent.setPackage("com.android.vending");
            }
            if (intent.resolveActivity(FlutterApplication.sApplication.getPackageManager()) != null) { //可以接收
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                FlutterApplication.sApplication.startActivity(intent);
            } else { //没有应用市场，我们通过浏览器跳转到Google Play
                intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=" + packName));
                //这里存在一个极端情况就是有些用户浏览器也没有，再判断一次
                if (intent.resolveActivity(FlutterApplication.sApplication.getPackageManager()) != null) { //有浏览器
                    FlutterApplication.sApplication.startActivity(intent);
                } else { //天哪，这还是智能手机吗？
                    ToastUtils.showShort("install google play");
                }
            }
        } catch (Exception e) {
        }

    }

}
